package com.selfdriving.utils;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

public class WriteCamera
{
	Context context;
	Activity activity;

	public WriteCamera(Context context, Activity activity)
	{
		this.context = context;
		this.activity = activity;
		write();
	}

	public void write()
	{
		new AlertDialog.Builder(context)
				.setTitle("请选择一张图片")
				.setItems(new String[] { "拍照", "从相册选取" },
						new DialogInterface.OnClickListener()
						{

							public void onClick(DialogInterface dialog,
									int which)
							{
								switch (which)
								{
								case 0:
									// dialog();
									getPhotoFromCamera();
									break;
								case 1:
									getImageFromGallery();
									// 从相册选取图片
									// Intent intent = new Intent(
									// Intent.ACTION_PICK, null);
									// intent.setDataAndType(
									// MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
									// "image/*");
									// startActivityForResult(intent,
									// SELECT_IMAGES_FROM_SD);
									break;
								// case 2:
								// startRelease(null, null);
								// break;
								}
							}
						})
				.setNegativeButton("取消", new DialogInterface.OnClickListener()
				{

					public void onClick(DialogInterface dialog, int which)
					{
						dialog.cancel();
					}
				}).create().show();
	}

	/** 获取相册照片 **/
	protected void getImageFromGallery()
	{
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		activity.startActivityForResult(
				Intent.createChooser(intent, "Complete action using"),
				Constant.PICK_FROM_FILE);
	}

	/** 获取拍照照片 **/
	protected void getPhotoFromCamera()
	{
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		Constant.imageCaptureUri = Uri.fromFile(new File(Environment
				.getExternalStorageDirectory() + "/Drive/", "tmp_"
				+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
				Constant.imageCaptureUri);
		try
		{
			intent.putExtra("return-data", true);
			activity.startActivityForResult(intent, Constant.PICK_FROM_CAMERA);
		} catch (ActivityNotFoundException e)
		{
			e.printStackTrace();
		}
	}

}
